import { INestApplication } from '@nestjs/common'
import { AppFactory } from './app.factory'
import * as pactum from 'pactum'

let appFactory: AppFactory
let app: INestApplication
// 在测试用例之前
global.beforeEach(async () => {
  //初始化app实例
  appFactory = await AppFactory.init()
  // 初始化数据库
  await appFactory?.destoryDB()
  await appFactory.initDB()
  //获取app实例
  app = appFactory.instance

  //全局化app实例
  pactum.request.setBaseUrl(await app.getUrl())
  global.pactum = pactum
  global.spec = pactum.spec()

  //   global.app = app
})

// 在测试用例之后
global.afterEach(async () => {
  //先清除数据库表内容再关闭进程
  await appFactory?.cleanupDB()
  // 在 NestJS 测试中用于关闭应用实例，释放资源
  await app?.close()
})
